<?php
namespace app\school\controller;
use think\Controller;
use think\Db;
use think\Session;
class Index extends Controller
{
    public function index()
    {
        return $this->fetch();
    }
    public function reg()
    {
        return $this->fetch();
    }
    public function reg1()
    {
       
        $school_phone = input('school_phone');
        $a = Db::name('school')->where('school_phone',$school_phone)->find();
        if($a == null)
        {
            
            $data1 = 
            [
                'school_phone' => input('school_phone'),
                'school_pw' => md5(input('school_pw')),
                'school_name' =>input('school_name'),
                'school_status' =>0,
                'school_time' =>date('Y-m-d H:i:s',time()),
            ];
            $res = Db::table('school')->insert($data1);
               if($res)
                {
                    return $this->success('注册成功','index/index');
                }
                else
                {
                    return $this->error('注册失败');
                }
        }
        else
        {
            return $this->error('账号已经被注册');
        }
       
    }
    public function login()
    {
        $data = [
            'school_phone' => input('school_phone'),
            'school_pw' => md5(input('school_pw')),
        ];
        // dump($data);
        // exit;
        $res = Db::name('school')->where($data)->find();
        // dump($res);
        // exit;
        if($res==null){
            return $this->error('账号密码错误');
        }else{
            Session::set("school_id",$res["school_id"]);
            $this->success('登录成功','school/index');
        }
    }
    public function logout(){
        Session::clear();
        $this->redirect("index/index");
    }
}
